import ContextPadProvider from 'bpmn-js/lib/features/context-pad/ContextPadProvider'
import { Injector } from 'didi'
import EventBus from 'diagram-js/lib/core/EventBus'
import ContextPad from 'diagram-js/lib/features/context-pad/ContextPad'
import Modeling from 'bpmn-js/lib/features/modeling/Modeling.js'
import ElementFactory from 'bpmn-js/lib/features/modeling/ElementFactory'
import Connect from 'diagram-js/lib/features/connect/Connect'
import Create from 'diagram-js/lib/features/create/Create'
import PopupMenu from 'diagram-js/lib/features/popup-menu/PopupMenu'
import Canvas from 'diagram-js/lib/core/Canvas'
import Rules from 'diagram-js/lib/features/rules/Rules'
import { Element, Shape } from 'diagram-js/lib/model/Types'
import { createVNode } from 'vue';
import { Modal } from 'ant-design-vue';
import Moddle from 'moddle';

class EnhancementContextPadProvider extends ContextPadProvider {
  private _contextPad: ContextPad
  private _modeling: Modeling
  private _elementFactory: ElementFactory
  private _autoPlace: any
  private _getContextPadEntries: any
  private _connect: Connect
  private _create: Create
  private _popupMenu: PopupMenu
  private _canvas: Canvas
  private _rules: Rules
  private _moddle: Moddle

  constructor(
    config: any,
    injector: Injector,
    eventBus: EventBus,
    contextPad: ContextPad,
    modeling: Modeling,
    elementFactory: ElementFactory,
    connect: Connect,
    create: Create,
    popupMenu: PopupMenu,
    canvas: Canvas,
    rules: Rules,
    translate
  ) {
    super(
      config,
      injector,
      eventBus,
      contextPad,
      modeling,
      elementFactory,
      connect,
      create,
      popupMenu,
      canvas,
      rules,
      translate,
      2000
    )

    this._contextPad = contextPad
    this._modeling = modeling
    this._elementFactory = elementFactory
    this._connect = connect
    this._create = create
    this._popupMenu = popupMenu
    this._canvas = canvas
    this._rules = rules
    this._moddle = injector.get('moddle');

    this._autoPlace = injector.get('autoPlace', false)
    this._getContextPadEntries = super.getContextPadEntries
  }

  getContextPadEntries(element: Element) {
    // if (element.type === 'bpmn:StartEvent') {
    //   return {}; // 返回空对象，不显示菜单
    // }

    //获取系统默认配置
    const defaultEntries = super.getContextPadEntries(element);
    console.log(defaultEntries,"默认配置111111111111")

    // 处理开始节点、结束节点 - 移除Connect按钮
    if (element.type === 'bpmn:EndEvent'||element.type === 'bpmn:StartEvent') {
      // 复制默认配置并删除connect条目
      const filteredEntries = { ...defaultEntries };
      delete filteredEntries.connect;
      //delete defaultEntries.connect;

      console.log(filteredEntries,"修改后的配置111111111111")
      console.log(defaultEntries,"默认配置222222222222222222")
      //return filteredEntries;
    }

    const actions: Record<string, any> = this._getContextPadEntries(element);

    if(element.type=="bpmn:SequenceFlow")
    {
      return actions;
    }

    // const appendUserTask = (event: Event, element: Shape) => {
    //   const shape = this._elementFactory.createShape({ type: 'bpmn:UserTask' })
    //   this._create.start(event, shape, {
    //     source: element
    //   })
    // }
    const createElement = (event: Event, type: string, options?: any) => {
      //并行网关时，同时创建两个
      if(type==="ParallelGateway"||type==="InclusiveGateway"){
        const elementFactory = this._elementFactory;
        const modeling = this._modeling;
        const parentElement = this._canvas.getRootElement();

        //创建第一个并行网关
        const gateway1 = this._elementFactory.createShape({
          type: 'bpmn:InclusiveGateway',
          x: 100,  
          y: 100   
        });
        //修改icon，暂时不行
        if (options) {
          //const newOptions={icon:'/src/assets/bpmnIcon/parallel-gateway.svg',...options}
          gateway1.businessObject.extended = options;
        }

        this._modeling.createShape(gateway1, { x: 100, y: 100 }, parentElement);

        //创建第二个并行网关
        const gateway2 = elementFactory.createShape({
          type: 'bpmn:ParallelGateway',
          x: 300,  
          y: 100  
        });

        if (options) {
          gateway2.businessObject.extended = {GatewayType:"ParallelGateway"};;
        }
        this._modeling.createShape(gateway2, { x: 300, y: 100 }, parentElement);
      }
      else{
        const shape = this._elementFactory.createShape({ type: `bpmn:${type}`, ...{} });
        if (options) {
          shape.businessObject.extended = options;
        }
        //如果是服务节点
        if(type==="ServiceTask"){
          // 创建 Activiti 扩展属性
          // const activitiExtension = this._moddle.create('activiti:class', {
          //   class: "com.zsix.system.service.listener.MachineService"
          // });
          // const extensionElements = this._moddle.create('bpmn:ExtensionElements', {
          //   values: [activitiExtension]
          // });
          shape.businessObject['activiti:class']="com.zsix.system.service.listener.MachineService";
          shape.businessObject.$attrs['activiti:class'] = "com.zsix.system.service.listener.MachineService";
          // shape.businessObject.extensionElements = extensionElements;
        }
        console.log(shape,"shape1111111111111111")
        this._create.start(event, shape, {
          source: element
        })
      }
    };

    const append = this._autoPlace
      ? (event: Event, element: Shape,options?: any,type?:any) => {
        //并行网关时，同时创建两个
      if(type==="ParallelGateway"||type==="InclusiveGateway"){
        const elementFactory = this._elementFactory;
        const modeling = this._modeling;
        const parentElement = this._canvas.getRootElement();

        //创建第一个并行网关
        const gateway1 = this._elementFactory.createShape({
          type: 'bpmn:InclusiveGateway',
          x: 100,  
          y: 100   
        });
        //修改icon，暂时不行
        if (options) {
          //const newOptions={icon:'/src/assets/bpmnIcon/parallel-gateway.svg',...options}
          gateway1.businessObject.extended = options;
        }
        this._autoPlace.append(element, gateway1)

        //创建第二个并行网关
        const gateway2 = elementFactory.createShape({
          type: 'bpmn:InclusiveGateway',
          x: 300,  
          y: 100  
        });

        if (options) {
          gateway2.businessObject.extended = {GatewayType:"JoinGateway"};;
        }
        this._autoPlace.append(gateway1, gateway2)
      }
      else{
        const shape = this._elementFactory.createShape({ type: `bpmn:${type}`, ...{} });
        if (options) {
          shape.businessObject.extended = options;
        }
        //如果是服务节点
        if(type==="ServiceTask"){
          // 创建 Activiti 扩展属性
          // const activitiExtension = this._moddle.create('activiti:class', {
          //   class: "com.zsix.system.service.listener.MachineService"
          // });
          // const extensionElements = this._moddle.create('bpmn:ExtensionElements', {
          //   values: [activitiExtension]
          // });
          shape.businessObject['activiti:class']="com.zsix.system.service.listener.MachineService";
          shape.businessObject.$attrs['activiti:class'] = "com.zsix.system.service.listener.MachineService";
          // shape.businessObject.extensionElements = extensionElements;
        }
        console.log(shape,"shape11111111111111122222222")
        this._autoPlace.append(element, shape)
      }
          
        }
      : createElement

      if(element.type!=="bpmn:StartEvent"&&element.type!=="bpmn:EndEvent"){
        // 执行节点
        actions['enhancement-op-1'] = {
          group: 'model',
          className: 'bpmn-icon-enhancement-op',
          title: '执行节点',
          action: {
            dragstart: e=>createElement(e, 'UserTask',{UserTaskType:"userTask"}),
            click: e=>append(e,...arguments,{UserTaskType:"userTask"},'UserTask')
          }
        }

        // 转换节点
        actions['enhancement-op-2'] = {
          group: 'model',
          className: 'bpmn-icon-enhancement-cc',
          title: '转换节点',
          action: {
            dragstart: e=>createElement(e, 'UserTask',{UserTaskType:"ccTask"}),
            click: e=>append(e,...arguments,{UserTaskType:"ccTask"},'UserTask')
          }
        }
        // //子流程节点
        // actions['enhancement-op-3'] = {
        //   group: 'model',
        //   className: 'bpmn-icon-enhancement-receive',
        //   title: '子流程节点',
        //   action: {
        //     dragstart: e=>createElement(e, 'ReceiveTask'),
        //     click: e=>append(e,...arguments,null,'ReceiveTask')
        //   }
        // }
        //赋值节点
        actions['enhancement-op-4'] = {
          group: 'model',
          className: 'bpmn-icon-enhancement-service',
          title: '赋值节点',
          action: {
            dragstart: e=>createElement(e, 'ServiceTask'),
            click: e=>append(e,...arguments,null,'ServiceTask')
          }
        }
        // //空结束节点
        // actions['enhancement-op-5'] = {
        //   group: 'model',
        //   className: 'bpmn-icon-enhancement-end',
        //   title: '空结束节点',
        //   action: {
        //     dragstart: e=>createElement(e, 'EndEvent'),
        //     click: e=>append(e,...arguments,null,'EndEvent')
        //   }
        // }
        //条件分支
        actions['enhancement-op-6'] = {
          group: 'model',
          className: 'bpmn-icon-enhancement-feel',
          title: '条件分支',
          action: {
            dragstart: e=>createElement(e, 'ExclusiveGateway'),
            click: e=>append(e,...arguments,{GatewayType:"feel"},'ExclusiveGateway')
          }
        }
        // //人工分支
        // actions['enhancement-op-7'] = {
        //   group: 'model',
        //   className: 'bpmn-icon-enhancement-hand',
        //   title: '人工分支',
        //   action: {
        //     dragstart: e=>createElement(e, 'ExclusiveGateway'),
        //     click: e=>append(e,...arguments,{GatewayType:"handSelect"},'ExclusiveGateway')
        //   }
        // }
        // //并行分支
        // actions['enhancement-op-8'] = {
        //   group: 'model',
        //   className: 'bpmn-icon-enhancement-parallel',
        //   title: '并行分支',
        //   action: {
        //     dragstart: e=>createElement(e, 'InclusiveGateway'),
        //     click: e=>append(e,...arguments,{GatewayType:"InclusiveGateway"},'InclusiveGateway')
        //   }
        // }
      }
      else{
        delete actions.connect;
        delete actions['append.text-annotation'];
      }

    // 添加一个新分组的自定义按钮
    const showConfirm = () => {
      Modal.confirm({
        title: () => 'Do you Want to delete these items?',
        // icon: () => createVNode(ExclamationCircleOutlined),
        content: () => createVNode('div', { style: 'color:red;' }, 'Some descriptions'),
        onOk() {
          console.log('OK');
        },
        onCancel() {
          console.log('Cancel');
        },
        class: 'test',
      });
    };
    // actions['enhancement-op'] = {
    //   group: 'enhancement',
    //   className: 'bpmn-icon-enhancement-op',
    //   title: '扩展操作2',
    //   action: {
    //     click: function (e: Event) {
    //       showConfirm()
    //       // alert('点击 扩展操作2')
    //     }
    //   }
    // }
    console.log(actions,"actions11111111111111111111111111111111")
    return  actions
  }
}

export default EnhancementContextPadProvider
